﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace CMS.Class
{
    public class clsFileUpLoad
    {
        public clsFileUpLoad()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public List<String[]> parseCSV(String path)
        {
            //TextReader 
            List<string[]> parseData = new List<string[]>();
            path = System.Web.HttpContext.Current.Server.MapPath("~/" + path);
            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;
                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split(',');
                        parseData.Add(row);
                    }
                }
            }
            catch (Exception e)
            {

            }
            return parseData;
        }

        public void DeleteFile(String path, string fileName)
        {
            try
            {
                String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
                filePath = filePath + fileName;
                System.IO.File.Delete(filePath);
            }
            catch
            {

            }
        }

        /// <summary>
        /// Ham xoa file
        /// </summary>
        /// <param name="fileName"></param>
        public void DeleteFile(string fileName)
        {
            try
            {
                String filePath = System.Web.HttpContext.Current.Server.MapPath("~/") + fileName.Trim();
                System.IO.File.Delete(filePath.Trim());
            }
            catch
            {

            }
        }

     

        public String FileSaveImage(String path, FileUpload fileName)
        {
            String saveFile = path + "/";
            String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
            if (fileName.FileName == "")
            {
                return "";
            }
            String str = "";
            if (fileName.HasFile)
            {
                String strNameMusic = fileName.FileName;
                strNameMusic = strNameMusic.ToLower();

                int i = -1;
                i = strNameMusic.LastIndexOf(".");
                try
                {
                    str = strNameMusic.Substring(i);
                    if (str.ToLower() == ".jpg" || str.ToLower() == ".jpeg" || str.ToLower() == ".gif" || str.ToLower() == ".png" || str.ToLower() == ".bmp")
                    {
                        str = System.Guid.NewGuid().ToString() + str;
                        str = DateTime.Now.ToString("yyyy_MM_dd") + "_" + str;
                        filePath = filePath + str;// fileName.FileName;
                        HttpPostedFile tenFile = fileName.PostedFile;
                        fileName.SaveAs(filePath);

                    }
                }
                catch
                {
                    str = "";
                }
            }
            if (str != "")
            {

                str = saveFile + str;
            }
            return str;
        }



        public String FileSave(String path, FileUpload fileName)
        {
            String saveFile = "~/" + path + "/";
            String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
            if (fileName.FileName == "")
            {
                return "";
            }
            String str = "";
            if (fileName.HasFile)
            {
                String strNameMusic = fileName.FileName;
                strNameMusic = strNameMusic.ToLower();

                int i = -1;
                i = strNameMusic.IndexOf(".mp3".ToLower());
                try
                {
                    str = strNameMusic.Substring(i);
                    if (str.ToLower() == ".mp3".ToLower())
                    {
                        str = System.Guid.NewGuid().ToString() + str;
                        str = DateTime.Now.ToString("yyyy_MM_dd") + "_" + str;
                        filePath = filePath + str;// fileName.FileName;
                        HttpPostedFile tenFile = fileName.PostedFile;
                        fileName.SaveAs(filePath);
                        saveFile += fileName.FileName;
                    }
                }
                catch
                {
                    str = path + str;
                }
            }
            return str;
        }

    }
}